home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / tnftpsr / tnmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-29  |  12.8 KB  |  687 lines

  1. /*     ANS compatible Telnet in AES windows     */
  2. /*         compiled with TURBO-C/PURE-C         */
  3. /*  P. Mayer & H. Wieser fortec TU Vienna 1992  */
  4.  
  5. #define noDEBUG
  6. #define NET
  7. #define GEMWIN
  8. #define DISPLAYSIZE    128
  9.  
  10. #include <stdio.h>    
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <tos.h>
  14. #include <aes.h>
  15. #include <ctype.h>
  16. #include "tcpdef.h"
  17. #include "tndefs.h"
  18.  
  19. #define INBUFSIZE 4096
  20. #define TELNET_PORT     23
  21. #define u_char unsigned char
  22. #define TERMINALTYPE    24
  23. #define TERMINALNAME    "VT100"
  24.  
  25. void printw(struct wi_str *wp, char *text);
  26. int upcall(struct wi_str *wp);
  27. void invert(struct wi_str *wp,int x1,int y1,int x2, int y2);
  28.  
  29. extern void w_output(struct wi_str *wp, unsigned char *ptr, short charcount);
  30. extern void w_info(struct wi_str *wp, char *name);
  31. extern long resolv(char *host, char ipaddr[4],struct wi_str *wp);
  32.  
  33. extern struct wi_str *wlist;  /* a pointer into the window ring */
  34. extern int delmode;
  35. extern struct wi_str *wsource;
  36.  
  37. char o_str[30];
  38. int o_len;
  39. char data[INBUFSIZE];
  40. DESTI desti, my;
  41. TCPSTAT tstat;
  42. char *myid = "@(#)TUW-Telnet 1.0 pm/hw";
  43.  
  44. static int tcp_id, state;
  45. static char *p;
  46. static char *term;
  47. static int tcp_buff = 4096;
  48. static int x;
  49. static int i;
  50.  
  51. /* open a connection and return tcp handle, 0 if error */
  52.  
  53. int connect(int myport, char * host, int port)
  54. {
  55.   term = (char *)getenv("TCPWND");
  56.   if(term) tcp_buff = atoi(term);
  57.  
  58.   desti.Port = port;
  59.  
  60. #ifdef NET
  61. /*  if(GetIPAddr(host,desti.IPAddr))
  62.   {
  63.     unsigned int tmp1,tmp2,tmp3,tmp4;
  64.     
  65.     if(sscanf(host,"%d.%d.%d.%d",&tmp1,&tmp2,&tmp3,&tmp4) != 4)
  66.     {
  67.       w_info(wlist,"<Error: unknown host.>");
  68.       wlist->pid = 0;
  69.       return 0;
  70.     }
  71.     desti.IPAddr[0] = tmp1;
  72.     desti.IPAddr[1] = tmp2;
  73.     desti.IPAddr[2] = tmp3;
  74.     desti.IPAddr[3] = tmp4;
  75.   }
  76. */
  77.  
  78.   if(resolv(host,desti.IPAddr,wlist))
  79.   {
  80.       w_info(wlist,"<Error: unknown host.>");
  81.       wlist->pid = 0;
  82.       return 0;
  83.   }
  84.  
  85.   w_info(wlist,"<Status: trying to open connection.>");
  86.   tcp_id = (unsigned)tcp_open(myport,&desti,AKTIV,60,(long)tcp_buff);
  87.  
  88.   if(tcp_id == 0)
  89.   {
  90.     w_info(wlist,"<Error: could not open connection.>");
  91.     wlist->pid = 0;
  92.     return 0;
  93.   }
  94.  
  95.   do
  96.   {
  97.     state = (int)tcp_stat(tcp_id,&tstat);
  98.     if((state > ESTABLISHED) || (state <= CLOSED)) break;
  99.     evnt_timer(10,0);
  100.   }
  101.   while(state < ESTABLISHED);
  102.  
  103.   if(state != ESTABLISHED)
  104.   {
  105.     w_info(wlist,"<Error: connection refused.>");
  106.     wlist->pid = 0;
  107.     return 0;
  108.   }
  109.   w_info(wlist,"<Status: connected.>");
  110.   return(tcp_id);
  111. #else
  112.   return 1;
  113. #endif
  114. }
  115.  
  116. int tnet(struct wi_str *wp, long code)
  117. {
  118.   char c;
  119.   int tcperr;
  120.   
  121.   c = (char)code;
  122.   o_len = 1;
  123.  
  124. #ifdef GEMWIN
  125.   switch((int)(code >> 8))
  126.   {
  127.     /* cursor keys... */
  128.  
  129.   case 0x0048:  /* up arrow */
  130.     if (wp->curskey)
  131.     {
  132.       o_str[0] = (int)ESC;
  133.       o_str[1] = (int)'O';
  134.       o_str[2] = (int)'A';
  135.       o_len = 3;
  136.  
  137.     }
  138.     else
  139.     {
  140.       o_str[0] = (int)ESC;
  141.       o_str[1] = (int)'[';
  142.       o_str[2] = (int)'A';
  143.       o_len = 3;
  144.     }
  145.     break;
  146.   case 0x0050:  /* down arrow */
  147.     if (wp->curskey)
  148.     {
  149.       o_str[0] = (int)ESC;
  150.       o_str[1] = (int)'O';
  151.       o_str[2] = (int)'B';
  152.       o_len = 3;
  153.     }
  154.     else
  155.     {
  156.       o_str[0] = (int)ESC;
  157.       o_str[1] = (int)'[';
  158.       o_str[2] = (int)'B';
  159.       o_len = 3;
  160.     }
  161.     break;
  162.   case 0x004d:  /* right arrow */
  163.     if (wp->curskey)
  164.     {
  165.       o_str[0] = (int)ESC;
  166.       o_str[1] = (int)'O';
  167.       o_str[2] = (int)'C';
  168.       o_len = 3;
  169.     }
  170.     else
  171.     {
  172.       o_str[0] = (int)ESC;
  173.       o_str[1] = (int)'[';
  174.       o_str[2] = (int)'C';
  175.       o_len = 3;
  176.     }
  177.     break;
  178.   case 0x004b:  /* left arrow */
  179.     if (wp->curskey)
  180.     {
  181.       o_str[0] = (int)ESC;
  182.       o_str[1] = (int)'O';
  183.       o_str[2] = (int)'D';
  184.       o_len = 3;
  185.     }
  186.     else
  187.     {
  188.       o_str[0] = (int)ESC;
  189.       o_str[1] = (int)'[';
  190.       o_str[2] = (int)'D';
  191.       o_len = 3;
  192.     }
  193.     break;
  194.  
  195.     /* wp->keypad keys... */
  196.  
  197.   case 0x0070:  /* 0 */
  198.     if (wp->keypad)
  199.     {
  200.       o_str[0] = (int)ESC;
  201.       o_str[1] = (int)'O';
  202.       o_str[2] = (int)'p';
  203.       o_len = 3;
  204.     }
  205.     else
  206.     {
  207.       o_str[0] = (int)'0';
  208.     }
  209.     break;
  210.   case 0x006d:  /* 1 */
  211.     if (wp->keypad)
  212.     {
  213.       o_str[0] = (int)ESC;
  214.       o_str[1] = (int)'O';
  215.       o_str[2] = (int)'q';
  216.       o_len = 3;
  217.     }
  218.     else
  219.     {
  220.       o_str[0] = (int)'1';
  221.     }
  222.     break;
  223.   case 0x006e:  /* 2 */
  224.     if (wp->keypad)
  225.     {
  226.       o_str[0] = (int)ESC;
  227.       o_str[1] = (int)'O';
  228.       o_str[2] = (int)'r';
  229.       o_len = 3;
  230.     }
  231.     else
  232.     {
  233.       o_str[0] = (int)'2';
  234.     }
  235.     break;
  236.   case 0x006f:  /* 3 */
  237.     if (wp->keypad)
  238.     {
  239.       o_str[0] = (int)ESC;
  240.       o_str[1] = (int)'O';
  241.       o_str[2] = (int)'s';
  242.       o_len = 3;
  243.     }
  244.     else
  245.     {
  246.       o_str[0] = (int)'3';
  247.     }
  248.     break;
  249.   case 0x006a:  /* 4 */
  250.     if (wp->keypad)
  251.     {
  252.       o_str[0] = (int)ESC;
  253.       o_str[1] = (int)'O';
  254.       o_str[2] = (int)'t';
  255.       o_len = 3;
  256.     }
  257.     else
  258.     {
  259.       o_str[0] = (int)'4';
  260.     }
  261.     break;
  262.   case 0x006b:  /* 5 */
  263.     if (wp->keypad)
  264.     {
  265.       o_str[0] = (int)ESC;
  266.       o_str[1] = (int)'O';
  267.       o_str[2] = (int)'u';
  268.       o_len = 3;
  269.     }
  270.     else
  271.     {
  272.       o_str[0] = (int)'5';
  273.     }
  274.     break;
  275.   case 0x006c:  /* 6 */
  276.     if (wp->keypad)
  277.     {
  278.       o_str[0] = (int)ESC;
  279.       o_str[1] = (int)'O';
  280.       o_str[2] = (int)'v';
  281.       o_len = 3;
  282.     }
  283.     else
  284.     {
  285.       o_str[0] = (int)'6';
  286.     }
  287.     break;
  288.   case 0x0067:  /* 7 */
  289.     if (wp->keypad)
  290.     {
  291.       o_str[0] = (int)ESC;
  292.       o_str[1] = (int)'O';
  293.       o_str[2] = (int)'w';
  294.       o_len = 3;
  295.     }
  296.     else
  297.     {
  298.       o_str[0] = (int)'7';
  299.     }
  300.     break;
  301.   case 0x0068:  /* 8 */
  302.     if (wp->keypad)
  303.     {
  304.       o_str[0] = (int)ESC;
  305.       o_str[1] = (int)'O';
  306.       o_str[2] = (int)'x';
  307.       o_len = 3;
  308.     }
  309.     else
  310.     {
  311.       o_str[0] = (int)'8';
  312.     }
  313.     break;
  314.   case 0x0069:  /* 9 */
  315.     if (wp->keypad)
  316.     {
  317.       o_str[0] = (int)ESC;
  318.       o_str[1] = (int)'O';
  319.       o_str[2] = (int)'y';
  320.       o_len = 3;
  321.     }
  322.     else
  323.     {
  324.       o_str[0] = (int)'9';
  325.     }
  326.     break;
  327.   case 0x004a:  /* - */
  328.     if (wp->keypad)
  329.     {
  330.       o_str[0] = (int)ESC;
  331.       o_str[1] = (int)'O';
  332.       o_str[2] = (int)'m';
  333.       o_len = 3;
  334.     }
  335.     else
  336.     {
  337.       o_str[0] = (int)'-';
  338.     }
  339.     break;
  340.   case 0x0072:  /* enter */
  341.     if (wp->keypad)
  342.     {
  343.       o_str[0] = (int)ESC;
  344.       o_str[1] = (int)'O';
  345.       o_str[2] = (int)'M';
  346.       o_len = 3;
  347.     }
  348.     else
  349.     {
  350.       o_str[0] = (int)RETURN;
  351.     }
  352.     break;
  353.   case 0x0071:  /* . */
  354.     if (wp->keypad)
  355.     {
  356.       o_str[0] = (int)ESC;
  357.       o_str[1] = (int)'O';
  358.       o_str[2] = (int)'n';
  359.       o_len = 3;
  360.     }
  361.     else
  362.     {
  363.       o_str[0] = (int)'.';
  364.     }
  365.     break;
  366.   case 0x004e:  /* + */
  367.     if (wp->keypad)
  368.     {
  369.       o_str[0] = (int)ESC;
  370.       o_str[1] = (int)'O';
  371.       o_str[2] = (int)'T';
  372.       o_len = 3;
  373.     }
  374.     else
  375.     {
  376.       o_str[0] = (int)'+';
  377.     }
  378.     break;
  379.   case 0x0063:  /* ( */
  380.     if (wp->keypad)
  381.     {
  382.       o_str[0] = (int)ESC;
  383.       o_str[1] = (int)'O';
  384.       o_str[2] = (int)'P';
  385.       o_len = 3;
  386.     }
  387.     else
  388.     {
  389.       o_str[0] = (int)'(';
  390.     }
  391.     break;
  392.   case 0x0064:  /* ) */
  393.     if (wp->keypad)
  394.     {
  395.       o_str[0] = (int)ESC;
  396.       o_str[1] = (int)'O';
  397.       o_str[2] = (int)'Q';
  398.       o_len = 3;
  399.     }
  400.     else
  401.     {
  402.       o_str[0] = (int)')';
  403.     }
  404.     break;
  405.   case 0x0065:  /* / */
  406.     if (wp->keypad)
  407.     {
  408.       o_str[0] = (int)ESC;
  409.       o_str[1] = (int)'O';
  410.       o_str[2] = (int)'R';
  411.       o_len = 3;
  412.     }
  413.     else
  414.     {
  415.       o_str[0] = (int)'/';
  416.     }
  417.     break;
  418.   case 0x0066:  /* * */
  419.     if (wp->keypad)
  420.     {
  421.       o_str[0] = (int)ESC;
  422.       o_str[1] = (int)'O';
  423.       o_str[2] = (int)'S';
  424.       o_len = 3;
  425.     }
  426.     else
  427.     {
  428.       o_str[0] = (int)'*';
  429.     }
  430.     break;
  431.  
  432.     /* anything else... */
  433.  
  434.   default:
  435.     switch(c)
  436.     {
  437.       case '\r':
  438.       {
  439.         if(wp->lfmode) 
  440.         {
  441.           o_str[0] = '\r';
  442.           o_str[1] = '\n';
  443.           o_len = 2;
  444.         }
  445.         else o_str[0] = '\r';
  446.         break;
  447.       }
  448.       case '\b':
  449.       {
  450.         if(!delmode)
  451.            o_str[0] = '\b';
  452.         else
  453.            o_str[0] = '\x7f';
  454.         break;
  455.       }
  456.       case '\x7f':
  457.       {
  458.         if(!delmode)
  459.            o_str[0] = '\x7f';
  460.         else
  461.            o_str[0] = '\b';
  462.         break;
  463.       }
  464.       default:
  465.       {
  466.         if(code && !c)
  467.            o_str[0] = '@';
  468.         else
  469.        o_str[0] = c;
  470.       }
  471.     }
  472.   }
  473.  
  474. #else
  475.   switch((int)(code >> 8))
  476.   {
  477.     /* cursor keys... */
  478.  
  479.   case 0x0048:  /* up arrow */
  480.       o_str[0] = (int)ESC;
  481.       o_str[1] = (int)'A';
  482.       o_len = 2;
  483.     break;
  484.   case 0x0050:  /* down arrow */
  485.       o_str[0] = (int)ESC;
  486.       o_str[1] = (int)'B';
  487.       o_len = 2;
  488.     break;
  489.   case 0x004d:  /* right arrow */
  490.       o_str[0] = (int)ESC;
  491.       o_str[1] = (int)'C';
  492.       o_len = 2;
  493.     break;
  494.   case 0x004b:  /* left arrow */
  495.       o_str[0] = (int)ESC;
  496.       o_str[1] = (int)'D';
  497.       o_len = 2;
  498.     break;
  499.   default:
  500.       o_str[0] = code & 0xff;
  501.       o_len = 1;
  502.   }
  503. #endif
  504.   if(c == 0x03)
  505.   {
  506.     o_str[0] = TN_IAC;
  507.     o_str[1] = TN_IP;
  508.     o_len = 2;
  509.   }
  510.       
  511.   tcperr = 0;
  512.   for(i=0; i <o_len;)
  513.   {
  514. #ifdef NET
  515.     x=(int)tcp_write(wp->pid,&o_str[i],o_len-i,PUSH,NO_URGENT);
  516. #else
  517.     x=1;
  518.     Bconout(AUX,o_str[i]);
  519. #endif
  520.     if(x >= 0)
  521.     {
  522.      i+=x;
  523.      tcperr = 0;
  524.     } 
  525.     else
  526.     {
  527.       w_info(wp,"<Error: in writing connection.>");
  528.       tcperr++;
  529.       if(tcperr > 10)
  530.       {
  531.        tcp_close(wp->pid);
  532.        wp->pid = 0;
  533.        break;
  534.       } 
  535.     }
  536.   }
  537.   if(c == 0x03)
  538.   {
  539.      while(tcp_read(wp->pid,&data,INBUFSIZE) > 0);
  540.   }
  541. #ifdef NET
  542.   state = (int)tcp_stat(wp->pid,&tstat);
  543.  
  544.   return state;
  545. #else
  546.   return 0;
  547. #endif
  548. }
  549.  
  550. int upcall(struct wi_str *wp)
  551. {
  552.   int length;
  553.   int x,xx;
  554.  
  555.   length = (int)tcp_read(wp->pid,&data,DISPLAYSIZE);
  556. #ifdef DEBUG
  557. printf("%d ",length);
  558. #endif
  559.   if(length <= 0) return length;
  560.   p = data;
  561.  
  562.   while(length)
  563.   {
  564.     if((u_char)p[0] == 255)
  565.     {
  566.       o_len = 0;
  567.       switch((u_char)p[1])
  568.       {
  569.       case TN_WILL:
  570.         o_str[o_len++]=TN_IAC;
  571.         if(p[2] == 1) o_str[o_len++]=TN_DO;
  572.         else o_str[o_len++]=TN_DONT;
  573.         o_str[o_len++]=p[2];
  574.         break;
  575.       case TN_WONT:
  576.         o_str[o_len++]=TN_IAC;
  577.         o_str[o_len++]=TN_WONT;
  578.         o_str[o_len++]=p[2];
  579.         break;
  580.       case TN_DO:
  581.         o_str[o_len++]=TN_IAC;
  582.         if(p[2] == 1 || p[2] == TERMINALTYPE)
  583.           o_str[o_len++]=TN_WILL;
  584.         else
  585.           o_str[o_len++]=TN_WONT;
  586.         o_str[o_len++]=p[2];
  587.         break;
  588.       case TN_DONT:
  589.         o_str[o_len++]=TN_IAC;
  590.         o_str[o_len++]=TN_WONT;
  591.         o_str[o_len++]=p[2];
  592.         break;
  593.       case TN_SB:
  594.         if(p[2] == TERMINALTYPE && p[3] == 1 )
  595.         {
  596.           o_str[o_len++]=TN_IAC;
  597.           o_str[o_len++]=TN_SB;
  598.           o_str[o_len++]=TERMINALTYPE;
  599.           o_str[o_len++]=0;
  600.           strcpy((char *)&o_str[o_len],TERMINALNAME);
  601.           o_len += (int)sizeof(TERMINALNAME)-1;
  602.           o_str[o_len++]=TN_IAC;
  603.           o_str[o_len++]=TN_SE;
  604.           length -= 3;
  605.           p += 3;
  606.           break;
  607.         }
  608.       case TN_IAC:
  609.         length -= 2;
  610.         p += 2;
  611.         Bconout(2,255);
  612.         continue;
  613.  
  614.       case TN_SE:
  615.       case TN_NOP:
  616.       case TN_DM:
  617.       case TN_BRK:
  618.       case TN_IP:
  619.       case TN_AO:
  620.       case TN_AYT:
  621.       case TN_EC:
  622.       case TN_EL:
  623.       case TN_GA:
  624.         p+=2;
  625.         length -= 2;
  626.         continue;
  627.       }
  628.       length -= 3;
  629.       x = 0;
  630.       while(o_len)
  631.       {
  632.         xx = (int)tcp_write(tcp_id,o_str+x,o_len,PUSH,NO_URGENT);
  633.         if(xx >= 0)
  634.         {
  635.           x+=xx;
  636.           o_len-=xx;
  637.         }
  638.         else
  639.           return(xx);
  640.       }
  641.       p+=3;
  642.     }
  643.     else
  644.     {
  645.       if(wp->lastx2 >= 0)
  646.       {
  647.        invert(wp,wp->markx1,wp->marky1,wp->markx2,wp->marky2);
  648.        wp->lastx2 = -1;
  649.       }
  650. #ifdef DEBUG
  651. printf("show %d chars\n",length);
  652. #endif      
  653.       w_output(wp,p,length);
  654.       length = 0;
  655.     }
  656.   }
  657.   return(length);
  658. }
  659.  
  660. int tcp_out(int tcp_id, char *o_str, int o_len)
  661. {
  662.   int xx = 0;
  663.   int x = 0;
  664.   
  665.        while(o_len)
  666.        {
  667. #ifdef NET
  668.          xx = (int)tcp_write(tcp_id,o_str+x,o_len,PUSH,NO_URGENT);
  669. #else
  670.          xx = 1;
  671.          Bconout(AUX,o_str[x]);
  672. #endif
  673.          if(xx >= 0)
  674.          {
  675.           x += xx;
  676.           o_len -= xx;
  677.          }
  678.          else break;
  679.        }
  680.        return o_len;
  681. }
  682.  
  683. void printw(struct wi_str *wp,char*text)
  684. {
  685.   w_output(wp,text,strlen(text));
  686. }
  687.